package com.ldc.common.net;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.TextUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class RequestLogStatusInterceptor implements Interceptor {
    private static final String TAG = "RequestLogStatusInterce";
    private final static int errorCode = -1001;//登录失败
    private final Handler mHandler;
    private static final int handle_code = 0x000;

    public RequestLogStatusInterceptor() {
        //
        HandlerThread handlerThread = new HandlerThread("LoginStatusInterceptor");
        handlerThread.start();
        Handler.Callback callback = msg -> {
            if (handle_code == msg.what) {
                String str_json = (String) msg.obj;
                if (TextUtils.isEmpty(str_json)) return false;
                return true;
            }
            return false;
        };
        mHandler = new Handler(handlerThread.getLooper(), callback);

    }


    @Override
    public Response intercept(Chain chain) throws IOException {
        final Request request = chain.request();
        final Response response = chain.proceed(request);
        final MediaType mediaType = response.body().contentType();
        final String content = response.body().string();
        //
        Message message = mHandler.obtainMessage(handle_code);
        message.obj = content;
        mHandler.sendMessage(message);
        //
        return response.newBuilder()
                .body(ResponseBody.create(mediaType, content))
                .build();
    }


}
